// 抽象
interface IStorage{
  save(key: string, value: string): void;
  read(key: string): any;
};
class UserInfo {
  constructor(public name: string, public storage: IStorage) {
    this.name = name;
  };
  save() {
    this.storage.save(this.name, JSON.stringify(this));
  };
  read() {
    return this.storage.read(this.name);
  }
};
class LocalStorage implements IStorage{
  save(key: string, value: any): void{
    console.log('localStorage.setItem',key,value);
    localStorage.setItem(key, value);
  };
  read(key: string): any{
    console.log('localStorage.getItem',key);
    return localStorage.getItem(key);
  }
};
let local = new LocalStorage();
let userInfo = new UserInfo('小明', local);
//存信息
userInfo.save();
// 取信息
let res4 = userInfo.read();
console.log('取信息', res4);
